<template>
  <div class="roles-container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片面板 -->
    <el-card class="box-card">
      <div style="margin-bottom: 15px;">
        <el-button type="primary">添加角色</el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column type="expand">
          <template slot-scope="scoped">
            <div style="border-bottom:1px  solid  #ccc">
              <el-row v-for="(item1,index1) in scoped.row.children" :key="index1">
                <el-col :span="8">
                  <div>
                    <!-- 左边 -->
                    <!-- 一级 -->
                    <el-tag closable>{{item1.authName}}</el-tag>
                    <span class="el-icon-caret-right"></span>
                  </div>
                </el-col>
                <el-col :span="16">
                  <div>
                    <!-- 右边 -->
                    <el-row v-for="(item2,index2) in item1.children" :key="index2">
                      <el-col :span="8">
                        <div>
                          <!-- 二级 -->
                          <el-tag type="warning" closable>{{item2.authName}}</el-tag>
                          <span class="el-icon-caret-right"></span>
                        </div>
                      </el-col>
                      <el-col :span="16">
                        <div>
                          <!-- 三级 -->
                          <el-tag
                            style="margin:5px;"
                            type="danger"
                            v-for="(item3,index3) in item2.children"
                            :key="index3"
                            closable
                          >{{item3.authName}}</el-tag>
                        </div>
                      </el-col>
                    </el-row>
                  </div>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <!-- 操作按钮 -->
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              @click="allotRoles(scoped.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisibleRoles">
      <el-tree
        ref="tree"
        :data="rolesdata"
        show-checkbox
        node-key="id"
        :default-checked-keys="defaultKey"
        :props="defaultProps"
        :default-expand-all="true"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRoles = false">取 消</el-button>
        <el-button type="primary" @click="addRightsDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
           
<script>
import { roles_api, rights_api, rolesToRights_api } from "@/api";
export default {
  created() {
    this.getTable();
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      //设置权限页面的隐藏
      dialogFormVisibleRoles: false,
      //树形控件的数据
      rolesdata: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      defaultKey: [], //  显示默认权限的  并且是第三级的id值
      roleParams: {
        roleId: "",
        rids: "",
      },
    };
  },
  methods: {
    //获取表格的数据
    async getTable() {
      const { data: res } = await roles_api();
      // console.log(res);
      this.tableData = res.data;
    },
    //分配权限的按钮
    async allotRoles(row) {
      this.roleParams.roleId = row.id; //  获取角色id
      this.defaultKey = []; //  每次进入就初始化之前选中的id值

      const { data: res } = await rights_api("tree");
      // console.log(res, 5666)
      this.rolesdata = res.data;
      //  递归函数 找row中的最里层的children  找到了后获取最里层的id  ，并将这个id  追加到  defaultKey数组中
      this.digui(row, this.defaultKey);
      this.dialogFormVisibleRoles = true;
    },
    //  封装的递归函数
    digui(row, arr) {
      if (!row.children) {
        return arr.push(row.id);
      }
      row.children.forEach((item) => {
        this.digui(item, arr);
      });
    },
    //分配权限页面确定按钮
    async addRightsDialog() {
      /* 获取rids===所选中的id值集合 */
      var keys = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys(),
      ];
      this.roleParams.rids = keys.join();
      // console.log(this.roleParams);
      //  发送请求角色授权
      const { data: res } = await rolesToRights_api(this.roleParams);
      // console.log(res, 3333);
      //判断请求是否成功
      if (res.meta.status === 200) {
        this.$message({
          showClose: true,
          message: res.meta.msg,
          type: "success",
        });
      } else {
        return this.$message({
          showClose: true,
          message: res.meta.msg,
          type: "success",
        });
      }
      //  重新获取数据
      this.getTable();
      //  关闭对话框
      this.dialogFormVisibleRoles = false;
    },
  },
};
</script>
<style scoped lang="less">
.roles-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
  .el-row {
    border-top: 1px solid #ccc;
    padding: 5px 0;
    &:nth-child(1) {
      border-top: 0px solid #000;
    }
  }
}
</style>